/*
 * @(#)DmsDefaultProfileSetting.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.dms.bean.DmsDefaultProfileSetting;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  DmsDefaultProfileSettingDAObject.java

  This class is the data access bean for table "DMS_DEFAULT_PROFILE_SETTING".

  @author      Rollo Chan
  @company     DCIVision Limited
  @creation date   02/01/2004
  @version     $Revision: 1.13.4.1 $
*/

public class DmsDefaultProfileSettingDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.13.4.1 $";

  public static final String TABLE_NAME = "DMS_DEFAULT_PROFILE_SETTING";

  public DmsDefaultProfileSettingDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("FIELD_NAME");
    this.vecDBColumn.add("FIELD_LABEL");
    this.vecDBColumn.add("FIELD_TYPE");
    this.vecDBColumn.add("MANDATORY_FLAG");
    
    this.vecDBColumn.add("VALUE_SUGGEST_FLAG");
    
    this.vecDBColumn.add("REFERENCE");
    this.vecDBColumn.add("SQL_STAT");
    this.vecDBColumn.add("DEFAULT_VALUE");
    this.vecDBColumn.add("USE_SYSTEM_DATE");
    this.vecDBColumn.add("DEFAULT_SELECT_VALUES");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FIELD_NAME, A.FIELD_LABEL, A.FIELD_TYPE, A.MANDATORY_FLAG, A.VALUE_SUGGEST_FLAG, A.REFERENCE, A.SQL_STAT, A.DEFAULT_VALUE, A.USE_SYSTEM_DATE , ");
        sqlStat.append("A.DEFAULT_SELECT_VALUES, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DEFAULT_PROFILE_SETTING A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = new DmsDefaultProfileSetting();
          tmpDmsDefaultProfileSetting.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDefaultProfileSetting.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpDmsDefaultProfileSetting.setFieldLabel(getResultSetString(rs, "FIELD_LABEL"));
          tmpDmsDefaultProfileSetting.setFieldType(getResultSetString(rs, "FIELD_TYPE"));
          tmpDmsDefaultProfileSetting.setMandatoryFlag(getResultSetString(rs, "MANDATORY_FLAG"));
          tmpDmsDefaultProfileSetting.setValueSuggestFlag(getResultSetString(rs, "VALUE_SUGGEST_FLAG"));
          tmpDmsDefaultProfileSetting.setReference(getResultSetString(rs, "REFERENCE"));
          tmpDmsDefaultProfileSetting.setSqlStat(getResultSetString(rs, "SQL_STAT"));
          tmpDmsDefaultProfileSetting.setDefaultValue(getResultSetString(rs,"DEFAULT_VALUE"));
          tmpDmsDefaultProfileSetting.setUseSystemDate(getResultSetString(rs,"USE_SYSTEM_DATE"));
          tmpDmsDefaultProfileSetting.setDefaultSelectValues(getResultSetString(rs,"DEFAULT_SELECT_VALUES"));
          tmpDmsDefaultProfileSetting.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDefaultProfileSetting.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDefaultProfileSetting.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDefaultProfileSetting.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDefaultProfileSetting.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDefaultProfileSetting.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
          tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
          return(tmpDmsDefaultProfileSetting);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FIELD_NAME, A.FIELD_LABEL, A.FIELD_TYPE, A.MANDATORY_FLAG, A.VALUE_SUGGEST_FLAG, A.REFERENCE, A.SQL_STAT, A.DEFAULT_VALUE, A.USE_SYSTEM_DATE, ");
        sqlStat.append("A.DEFAULT_SELECT_VALUES, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DEFAULT_PROFILE_SETTING A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }*/
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = new DmsDefaultProfileSetting();
          tmpDmsDefaultProfileSetting.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDefaultProfileSetting.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpDmsDefaultProfileSetting.setFieldLabel(getResultSetString(rs, "FIELD_LABEL"));
          tmpDmsDefaultProfileSetting.setFieldType(getResultSetString(rs, "FIELD_TYPE"));
          tmpDmsDefaultProfileSetting.setMandatoryFlag(getResultSetString(rs, "MANDATORY_FLAG"));
          tmpDmsDefaultProfileSetting.setValueSuggestFlag(getResultSetString(rs, "VALUE_SUGGEST_FLAG"));
          tmpDmsDefaultProfileSetting.setReference(getResultSetString(rs, "REFERENCE"));
          tmpDmsDefaultProfileSetting.setSqlStat(getResultSetString(rs, "SQL_STAT"));
          tmpDmsDefaultProfileSetting.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpDmsDefaultProfileSetting.setUseSystemDate(getResultSetString(rs, "USE_SYSTEM_DATE"));
          tmpDmsDefaultProfileSetting.setDefaultSelectValues(getResultSetString(rs, "DEFAULT_SELECT_VALUES"));
          tmpDmsDefaultProfileSetting.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDefaultProfileSetting.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDefaultProfileSetting.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDefaultProfileSetting.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDefaultProfileSetting.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDefaultProfileSetting.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
          tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
          tmpDmsDefaultProfileSetting.setRecordCount(totalNumOfRecord);
          tmpDmsDefaultProfileSetting.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsDefaultProfileSetting);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.FIELD_NAME, A.FIELD_LABEL, A.FIELD_TYPE, A.MANDATORY_FLAG, A.VALUE_SUGGEST_FLAG, A.REFERENCE, A.SQL_STAT,A.DEFAULT_VALUE, A.USE_SYSTEM_DATE, ");
        sqlStat.append("A.DEFAULT_SELECT_VALUES, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_DEFAULT_PROFILE_SETTING A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = new DmsDefaultProfileSetting();
          tmpDmsDefaultProfileSetting.setID(getResultSetInteger(rs, "ID"));
          tmpDmsDefaultProfileSetting.setFieldName(getResultSetString(rs, "FIELD_NAME"));
          tmpDmsDefaultProfileSetting.setFieldLabel(getResultSetString(rs, "FIELD_LABEL"));
          tmpDmsDefaultProfileSetting.setFieldType(getResultSetString(rs, "FIELD_TYPE"));
          tmpDmsDefaultProfileSetting.setMandatoryFlag(getResultSetString(rs, "MANDATORY_FLAG"));
          tmpDmsDefaultProfileSetting.setValueSuggestFlag(getResultSetString(rs, "VALUE_SUGGEST_FLAG"));
          tmpDmsDefaultProfileSetting.setReference(getResultSetString(rs, "REFERENCE"));
          tmpDmsDefaultProfileSetting.setSqlStat(getResultSetString(rs, "SQL_STAT"));
          tmpDmsDefaultProfileSetting.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
          tmpDmsDefaultProfileSetting.setUseSystemDate(getResultSetString(rs, "USE_SYSTEM_DATE"));
          tmpDmsDefaultProfileSetting.setDefaultSelectValues(getResultSetString(rs, "DEFAULT_SELECT_VALUES"));
          tmpDmsDefaultProfileSetting.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsDefaultProfileSetting.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsDefaultProfileSetting.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsDefaultProfileSetting.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsDefaultProfileSetting.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsDefaultProfileSetting.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
          tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
          result.add(tmpDmsDefaultProfileSetting);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = (DmsDefaultProfileSetting)((DmsDefaultProfileSetting)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_DEFAULT_PROFILE_SETTING(ID, FIELD_NAME, FIELD_LABEL, FIELD_TYPE, MANDATORY_FLAG, VALUE_SUGGEST_FLAG, REFERENCE, SQL_STAT, DEFAULT_VALUE, USE_SYSTEM_DATE, ");
        sqlStat.append("DEFAULT_SELECT_VALUES, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsDefaultProfileSetting.getFieldName());
        setPrepareStatement(preStat, 3, tmpDmsDefaultProfileSetting.getFieldLabel());
        setPrepareStatement(preStat, 4, tmpDmsDefaultProfileSetting.getFieldType());
        setPrepareStatement(preStat, 5, tmpDmsDefaultProfileSetting.getMandatoryFlag());
        setPrepareStatement(preStat, 6, tmpDmsDefaultProfileSetting.getValueSuggestFlag());
        setPrepareStatement(preStat, 7, tmpDmsDefaultProfileSetting.getReference());
        setPrepareStatement(preStat, 8, tmpDmsDefaultProfileSetting.getSqlStat());
        setPrepareStatement(preStat, 9, tmpDmsDefaultProfileSetting.getDefaultValue());
        setPrepareStatement(preStat, 10, tmpDmsDefaultProfileSetting.getUseSystemDate());
        setPrepareStatement(preStat, 11, tmpDmsDefaultProfileSetting.getDefaultSelectValues());
        setPrepareStatement(preStat, 12, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 13, new Integer(0));
        setPrepareStatement(preStat, 14, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 15, currTime);
        setPrepareStatement(preStat, 16, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 17, currTime);
        preStat.executeUpdate();
        tmpDmsDefaultProfileSetting.setID(nextID);
        tmpDmsDefaultProfileSetting.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsDefaultProfileSetting.setCreateDate(currTime);
        tmpDmsDefaultProfileSetting.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsDefaultProfileSetting.setUpdateDate(currTime);
        tmpDmsDefaultProfileSetting.setUpdateCount(new Integer(0));
        tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
        tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
        return(tmpDmsDefaultProfileSetting);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = (DmsDefaultProfileSetting)((DmsDefaultProfileSetting)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_DEFAULT_PROFILE_SETTING ");
        sqlStat.append("SET  FIELD_NAME=?, FIELD_LABEL=?, FIELD_TYPE=?, MANDATORY_FLAG=?, VALUE_SUGGEST_FLAG=?, REFERENCE=?, SQL_STAT=?, DEFAULT_VALUE=? , USE_SYSTEM_DATE=? , ");
        sqlStat.append("DEFAULT_SELECT_VALUES=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsDefaultProfileSetting.getFieldName());
        setPrepareStatement(preStat, 2, tmpDmsDefaultProfileSetting.getFieldLabel());
        setPrepareStatement(preStat, 3, tmpDmsDefaultProfileSetting.getFieldType());
        setPrepareStatement(preStat, 4, tmpDmsDefaultProfileSetting.getMandatoryFlag());
        setPrepareStatement(preStat, 5, tmpDmsDefaultProfileSetting.getValueSuggestFlag());
        setPrepareStatement(preStat, 6, tmpDmsDefaultProfileSetting.getReference());
        setPrepareStatement(preStat, 7, tmpDmsDefaultProfileSetting.getSqlStat());
        setPrepareStatement(preStat, 8, tmpDmsDefaultProfileSetting.getDefaultValue()==null?"":tmpDmsDefaultProfileSetting.getDefaultValue());
        setPrepareStatement(preStat, 9, tmpDmsDefaultProfileSetting.getUseSystemDate()==null?"":tmpDmsDefaultProfileSetting.getUseSystemDate());
        setPrepareStatement(preStat, 10, tmpDmsDefaultProfileSetting.getDefaultSelectValues()==null?"":tmpDmsDefaultProfileSetting.getDefaultSelectValues());
        setPrepareStatement(preStat, 11, new Integer(tmpDmsDefaultProfileSetting.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        setPrepareStatement(preStat, 14, tmpDmsDefaultProfileSetting.getID());
        setPrepareStatement(preStat, 15, tmpDmsDefaultProfileSetting.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsDefaultProfileSetting.setDefaultValue(tmpDmsDefaultProfileSetting.getDefaultValue()==null?"":tmpDmsDefaultProfileSetting.getDefaultValue());
          tmpDmsDefaultProfileSetting.setDefaultSelectValues(tmpDmsDefaultProfileSetting.getDefaultSelectValues()==null?"":tmpDmsDefaultProfileSetting.getDefaultSelectValues());
          tmpDmsDefaultProfileSetting.setUseSystemDate(tmpDmsDefaultProfileSetting.getUseSystemDate()==null?"":tmpDmsDefaultProfileSetting.getUseSystemDate());
          tmpDmsDefaultProfileSetting.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsDefaultProfileSetting.setUpdateDate(currTime);
          tmpDmsDefaultProfileSetting.setUpdateCount(new Integer(tmpDmsDefaultProfileSetting.getUpdateCount().intValue() + 1));
          tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
          tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
          return(tmpDmsDefaultProfileSetting);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = (DmsDefaultProfileSetting)((DmsDefaultProfileSetting)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_DEFAULT_PROFILE_SETTING ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsDefaultProfileSetting.getID());
        setPrepareStatement(preStat, 2, tmpDmsDefaultProfileSetting.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsDefaultProfileSetting);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = (DmsDefaultProfileSetting)this.oldValue;

    if (tmpDmsDefaultProfileSetting != null) {
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldName()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldLabel()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldType()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getMandatoryFlag()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getReference()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getSqlStat()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getDefaultValue()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUseSystemDate()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getDefaultSelectValues()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdateDate()));
    }

    tmpDmsDefaultProfileSetting = (DmsDefaultProfileSetting)obj;
    if (tmpDmsDefaultProfileSetting != null) {
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getID()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldName()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldLabel()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getFieldType()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getMandatoryFlag()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getReference()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getSqlStat()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getDefaultValue()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUseSystemDate()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getDefaultSelectValues()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsDefaultProfileSetting.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  
 /**
  * Gets the default profile setting by ID.
  * 
  * @param id the default profile setting ID
  * @return the DmsDefaultProfileSetting object
  * @throws ApplicationException
  */ 
  public synchronized AbstractBaseObject getDPFByID(Integer id) throws ApplicationException {
     PreparedStatement preStat = null;
     ResultSet rs = null;
     StringBuffer sqlStat = new StringBuffer();

     synchronized(dbConn) {
       try {
         sqlStat.append("SELECT A.ID, A.FIELD_NAME, A.FIELD_LABEL, A.FIELD_TYPE, A.MANDATORY_FLAG, A.VALUE_SUGGEST_FLAG, A.REFERENCE, A.SQL_STAT, A.DEFAULT_VALUE, A.USE_SYSTEM_DATE, ");
         sqlStat.append("A.DEFAULT_SELECT_VALUES, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
         sqlStat.append("FROM   DMS_DEFAULT_PROFILE_SETTING A ");
         sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
         preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         this.setPrepareStatement(preStat, 1, id);
         this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
         rs = preStat.executeQuery();
         if (rs.next()) {
           DmsDefaultProfileSetting tmpDmsDefaultProfileSetting = new DmsDefaultProfileSetting();
           tmpDmsDefaultProfileSetting.setID(getResultSetInteger(rs, "ID"));
           tmpDmsDefaultProfileSetting.setFieldName(getResultSetString(rs, "FIELD_NAME"));
           tmpDmsDefaultProfileSetting.setFieldLabel(getResultSetString(rs, "FIELD_LABEL"));
           tmpDmsDefaultProfileSetting.setFieldType(getResultSetString(rs, "FIELD_TYPE"));
           tmpDmsDefaultProfileSetting.setMandatoryFlag(getResultSetString(rs, "MANDATORY_FLAG"));
           tmpDmsDefaultProfileSetting.setValueSuggestFlag(getResultSetString(rs, "VALUE_SUGGEST_FLAG"));
           tmpDmsDefaultProfileSetting.setReference(getResultSetString(rs, "REFERENCE"));
           tmpDmsDefaultProfileSetting.setSqlStat(getResultSetString(rs, "SQL_STAT"));
           tmpDmsDefaultProfileSetting.setDefaultValue(getResultSetString(rs, "DEFAULT_VALUE"));
           tmpDmsDefaultProfileSetting.setUseSystemDate(getResultSetString(rs, "USE_SYSTEM_DATE"));
           tmpDmsDefaultProfileSetting.setDefaultSelectValues(getResultSetString(rs, "DEFAULT_SELECT_VALUES"));
           tmpDmsDefaultProfileSetting.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
           tmpDmsDefaultProfileSetting.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
           tmpDmsDefaultProfileSetting.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
           tmpDmsDefaultProfileSetting.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
           tmpDmsDefaultProfileSetting.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
           tmpDmsDefaultProfileSetting.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
           tmpDmsDefaultProfileSetting.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getCreatorID()));
           tmpDmsDefaultProfileSetting.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsDefaultProfileSetting.getUpdaterID()));
           return(tmpDmsDefaultProfileSetting);
         } else {
           throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
         }
       } catch (ApplicationException appEx) {
         throw appEx;
       } catch (SQLException sqle) {
         log.error(sqle, sqle);
         throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
       } catch (Exception e) {
         log.error(e, e);
         throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
       } finally {
         try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
         try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
       }
     }
   }
  
  /**
   * Gets the max UPDATE_DATE record.
   * 
   * 
   * @return the max update date
   * @throws ApplicationException
   */  
  
  public synchronized Timestamp getMaxUpdateDate(String tableName,Integer ID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Timestamp maxUpdateDate = null;
    if (tableName==""){
      tableName = "DMS_DEFAULT_PROFILE_SETTING";      
    }
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT max(UPDATE_DATE) as UPDATE_DATE  ");
        sqlStat.append("FROM   "+tableName+" ");
        if( ID!= null && ID.intValue() != 0){
          sqlStat.append(" where ID =  "+ID+" ");          
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        //this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);        
        rs = preStat.executeQuery();
        if (rs.next() ) {          
          maxUpdateDate = getResultSetTimestamp(rs, "UPDATE_DATE");
        }        
        return maxUpdateDate;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
}
